package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    private SpecGroupService specGroupService;

    @Autowired
    private SpecParamService specParamService;

    /**
     * 根据商品分类id 获取对应的规格参数组集合
     * @param cid
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupsByCategory(@RequestParam("id") Long cid) {


        return ResponseEntity.ok(specGroupService.queryGroupsByCategory(cid));
    }

    /**
     * 根据规格参数组id 获取对应的 规格参数信息
     * 规格参数的条件查询
     * @param gid
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam(value = "groupId",required = false) Long gid,
            @RequestParam(value = "categoryId",required = false) Long cid,
            @RequestParam(value = "searching",required = false) Boolean searching

    ) {

        return ResponseEntity.ok(specParamService.querySpecParams(gid,cid,searching));
    }

    /**
     * 新增规格参数组
     * @param specGroupDTO
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> addSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {

        specGroupService.addSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     *  修改规格组参数
     * @param specGroupDTO
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updateSpeGroup(@RequestBody SpecGroupDTO specGroupDTO) {

        specGroupService.updateSpeGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 新增 规格参数
     * @param specParamDTO
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity<Void> addSpecParam(@RequestBody SpecParamDTO specParamDTO) {

        specParamService.addSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改参数规格
     * @param specParamDTO
     * @return
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParamDTO specParamDTO) {

        specParamService.updateSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据分类id查询规格组及组内参数
     * @return 规格组集合
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategory(@RequestParam("id") Long categoryId) {
        return ResponseEntity.ok(specGroupService.queryGroupsByCategory(categoryId));
    }

    /**
     *  根据分类id查询对应的规格参数组 以及组内参数信息
     * @param cid
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecList(@RequestParam("id")Long cid) {

        return ResponseEntity.ok(this.specGroupService.querySpecList(cid));
    }

}
